const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");


router.post('/scan/findCargo', async ctx => {
    let tenantId = ctx.user.tenantId;
    let code = ctx.request.body.code;
    let iidArr = code.split('-')
    if(iidArr.length !== 2) return ctx.err('二维码读取失败')
    let sql = `select top 1 cargoId from cargo_invoice where tenantId=${tenantId} and iid=${iidArr[1]} `
    let car = await db.query(sql, {type: QueryTypes.SELECT, plain: true})
    if(!car) return ctx.err('该单据未有装车记录')

    let cargo = await db.models['cargo'].findOne({
        where: {
            id: car.cargoId
        }
    })
    return ctx.suc('', cargo)
})



module.exports = router;
